import Stack from "./stack";

// 十进制转任意进制（2~36）
const dec2any = (num, base) => {
  if (base < 2 || base > 36) {
    throw new TypeError("只支持转换为2~36进制的数字");
  }

  const stack = new Stack();
  const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let res = ``,
    rem = 0;

  while (num > 0) {
    rem = num % base;
    stack.push(rem);
    num = Math.floor(num / base);
  }

  while(!stack.isEmpty()) {
    res += digits[stack.pop()]
  }

  return res
};

export default dec2any